//package com.example.r2022913.Intercepter;
//
//import cn.hutool.core.util.StrUtil;
//import com.auth0.jwt.JWT;
//import com.auth0.jwt.JWTVerifier;
//import com.auth0.jwt.algorithms.Algorithm;
//import com.auth0.jwt.exceptions.JWTDecodeException;
//import com.auth0.jwt.exceptions.JWTVerificationException;
//import com.example.r2022913.exception.ExceptionEnum;
//import com.example.r2022913.exception.SelfException;
//import com.example.r2022913.model.entity.TestEntity;
//import com.example.r2022913.service.TestService;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Component;
//import org.springframework.web.method.HandlerMethod;
//import org.springframework.web.servlet.HandlerInterceptor;
//import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
//
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;
//
//@Component
//public class JwtInterceptor extends InterceptorRegistry implements HandlerInterceptor {
//    @Autowired
//    private TestService userService;
//
//    public static TestEntity user;
//
//
//    public boolean preHandle(HttpServletRequest httpServletRequest,
//                             HttpServletResponse httpServletResponse, Object object) throws Exception{
//
//        // 跨域全局配置/**/
//
//        httpServletResponse.setHeader("Access-control-Allow-Origin", httpServletRequest.getHeader("Origin"));
//        httpServletResponse.setHeader("Access-Control-Allow-Methods", "GET,POST,OPTIONS,PUT,DELETE");
//        httpServletResponse.setHeader("Access-Control-Allow-Headers", httpServletRequest.getHeader("Access-Control-Request-Headers"));
//        // 跨域时会首先发送一个OPTIONS请求，这里我们给OPTIONS请求直接返回正常状态
//
//        //获取token
//       String token = httpServletRequest.getHeader("token");
//
//        // 如果不是映射到方法直接通过
//        if(!(object instanceof HandlerMethod)){
//            return true;
//        }
//        // 执行认证
//        if (StrUtil.isBlank(token)) {
//            throw new SelfException(ExceptionEnum.TOKEN_FAIL);
//        }
//
//        //获取token的数据库中对应表的id
//        String userId;
//
//        try {
//            //解密获取
//            userId = JWT.decode(token).getAudience().get(0); //得到token中的userid载荷
//
//        } catch (JWTDecodeException j) {
//            throw new SelfException(ExceptionEnum.TOKEN_FAIL);
//        }
//
//        //根据id查询数据库
//        TestEntity user1 = userService.getById(userId);
//
//        if(user1 == null){
//            throw new SelfException(ExceptionEnum.TOKEN_FAIL);
//        }
//
//        // 用户密码加签验证 token
//        JWTVerifier jwtVerifier = JWT.require(Algorithm.HMAC256(user1.getPassword())).build();
//        try {
//            jwtVerifier.verify(token);
//        } catch (JWTVerificationException e) {
//            throw new SelfException(ExceptionEnum.TOKEN_FAIL);
//        }
//
//        //给全局user赋值 便于后面全局验证权限使用
//        user = user1;
//
//        return true;
//    }
//}
